<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Rate Professor</title>
</head>
<body>
<%@ page import="tuna.Process" %>
<%@ page import="tuna.DBConnection" %>
<%@ page import="java.sql.ResultSet;" %>
<%
	String day = request.getParameter("day");
	String us = request.getParameter("us");
	String role = request.getParameter("role");
	String id = request.getParameter("id");
	
	if(Process.validate(day, us, role , id)) {
		String parameters = "?day=" + day + "&us=" + us + "&role=" + role + "&id=" + id;
%>
	<table align="center" style="font-family: arial; width: 480px;">
		<tr>
			<td>
				Course Name			
			</td>
			<td>
				Professor
			</td>
		</tr>
	<%		
		//Create DB connection & connect
	    DBConnection dbCon = new DBConnection("localhost:3306", "tuna", "root", "asdf1234");
	    dbCon.connectDB();
	    
	    //Retrieve result set
	    String query = "select prof.userId, prof.profName, prof.courseName from " + 
	    	"(select userId, userRecord.name as profName, courseRecord.name as courseName, session as profSession from " +
	    	"courseRecord inner join userRecord on courseRecord.userId = userRecord.Id where userRecord.isProf=1) as prof inner join " + 
	    	"(select name, session from (select courseRecord.name as name, session, " +
	    	"utorId from tuna.courseRecord inner join tuna.userRecord on courseRecord.userId = userRecord.id) as courseUser " +
	    	"where utorId=\'" + us + "\') as user " +
	    	"on prof.courseName=user.name and prof.profSession=user.session";
	    
	    ResultSet courseProf = dbCon.runQuery(query);
			
		while(courseProf.next()) {
			String profId = courseProf.getString(1);
			String profName = courseProf.getString(2);
			String courseName = courseProf.getString(3);
			
			String prof_param = parameters + "&profId=" + profId + "&profName=" + profName + "&courseName=" + courseName;
	%>	
		<tr>
			<td>
				<%= courseName %>
			</td>
			<td>
				<a href="/tuna/rateProf.jsp<%= prof_param %>"><%= profName%></a>
			</td>
		</tr>
	<%		
		}
		
		dbCon.disconnectDB();
	%>
	</table>

<% 
	}
	else {
		response.sendRedirect("/tuna/login.jsp");
	}
%>
</body>
</html>